﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.ViewModels;
using Fashion.Models;
using Valentica.Libraries;
using System.Net;

namespace Fashion.Controllers
{
    public class SearchController : ThemeController
    {
        int ProductsPerPage = 9;
        FashionEntities1 FashionDB = new FashionEntities1();
        //
        // GET: /Search/
        public ActionResult Index()
        {
            var viewModel = new SearchCriteria();

            return View(viewModel);
        }

        public ActionResult Basic(string keyword, int page = 1)
        {
            if (keyword == null)
                keyword = Request.Form["ctl00$keyword"];

            Pagination pagination = new Pagination(true);
            string link = "/Search/Basic?keyword=" + keyword;
            pagination.BaseUrl = link;
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            StoreIndexViewModel storeIndex = new StoreIndexViewModel();
            var products = storeIndex.SearchForProducts(keyword,start,offset);

            int totalProducts = storeIndex.totalProducts;
            pagination.TotalRows = totalProducts;
            string pageLinks = pagination.GetPageLinks();

            // Find category
            int[] cat = new int[products.Count];
            for (int i = 0; i < cat.Length; i++)
            {
                cat[i] = products[i].CategoryID;
            }

            int[] cats = cat.Distinct().ToArray();
            var categories = storeIndex.GetCategoriesByProduct(cats);

            var viewModel = new StoreIndexViewModel
            {
                Products = products,
                Categories = categories
            };

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            return View(viewModel);
        }

        public ActionResult Advance(SearchCriteria criteria, int page=1)
        {
            Pagination pagination = new Pagination(true);
            string link = "Advance?criteria";
            pagination.BaseUrl = link;
            pagination.CurPage = page;
            pagination.PerPage = ProductsPerPage;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            pagination.LoadPaginationXml("\\Content\\Pagination.xml");

            int start = (page - 1) * pagination.PerPage;
            int offset = pagination.PerPage;

            StoreIndexViewModel storeIndex = new StoreIndexViewModel();

            var products = storeIndex.SearchByCriteria(criteria, start, offset);

            int totalProducts = storeIndex.totalProducts;
            pagination.TotalRows = totalProducts;
            string pageLinks = pagination.GetPageLinks();

            int[] cat = new int[products.Count];
            for (int i = 0; i < cat.Length; i++)
            {
                cat[i] = products[i].CategoryID;
            }

            int[] cats = cat.Distinct().ToArray();
            var categories = storeIndex.GetCategoriesByProduct(cats);

            var viewModel = new StoreIndexViewModel
            {
                Products = products,
                Categories = categories
            };

            ViewData["totalProducts"] = totalProducts;
            ViewData["products"] = products;
            ViewData["pageLinks"] = pageLinks;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            string keyword = form["ctl00$keyword"];
            if (string.IsNullOrEmpty(keyword))
            {
                string productKeyword = form["ctl00$MainContent$product"];
                string brandKeyword = form["brand"];
                string categoryKeyword = form["category"];
                string priceKeyword = form["ctl00$MainContent$price"];
                string ratingKeyword = form["ctl00$MainContent$rating"];

                return RedirectToAction("Advance", new SearchCriteria
                {
                    productName = productKeyword,
                    category = categoryKeyword,
                    brandName = brandKeyword
                    ,
                    price = priceKeyword,
                    rating = ratingKeyword
                });
            }

            return RedirectToAction("Basic", "Search", new { keyword = keyword });
        }

        [HttpPost]
        public ActionResult Basic(FormCollection form)
        {
            string productKeyword = form["ctl00$keyword"];

            return RedirectToAction("Basic", "Search", new { keyword = productKeyword });
        }

        [HttpPost]
        public ActionResult Advance(FormCollection form)
        {
            string productKeyword = form["ctl00$keyword"];

            return RedirectToAction("Basic", "Search", new { keyword = productKeyword });
        }
    }
}
